from flask import Blueprint, request, session, render_template, redirect, send_from_directory, url_for
from services import login_service, mail_service
from utils import JsonResponse
import os

bp = Blueprint('login', __name__)

from flask import request, jsonify


@bp.route('/register', methods=["GET", "POST"])
def register():
    if request.method == "POST":
        # 使用 request.get_json() 获取 JSON 数据
        data = request.get_json()  # 从 JSON 请求体获取数据
        name = data.get('name')
        username = data.get('username')  # 获取 username
        password = data.get('password')  # 获取 password

        print(name, username, password)  # 打印接收到的数据

        if not (name and username and password):
            return JsonResponse.error("服务端未理解提交数据1")

        status, info = login_service.register(name, username, password)

        if status:
            return JsonResponse.success(info.msg)
        else:
            return JsonResponse.error(info.msg)
    else:
        return JsonResponse.success("GET请求打开成功")


@bp.route('/user/login', methods=["GET", "POST"])
def login():
    if request.method == "POST":
        json = request.json
        username = json.get("username")
        password = json.get("password")
        if not (username and password):
            return JsonResponse.error("服务器未理解数据")

        status, info = login_service.login(username, password)

        if status:
            return JsonResponse.success(info.msg, info.data)
        else:
            return JsonResponse.error(info.msg)

    else:
        return send_from_directory(os.path.join('static', 'paper', 'page', 'login'), 'login.html')


@bp.route('/user/logout', methods=["GET", "POST"])
def logout():
    session.clear()
    # return redirect("/login")
    # return Response(200, "登出成功")
    return JsonResponse.success("登出成功")
    # return redirect(url_for("login.login"))


@bp.route('/edit_pwd', methods=["GET", "POST"])
def edit_pwd():
    if request.method == "POST":
        json = request.json
        username = json.get("username", None)
        id = json.get("id")
        password = json.get("password")
        # print(username, email, password)
        # if not (email and password):
        #     # return Response(400, "服务端未理解数据")
        #     return JsonResponse.error("服务端未理解数据3")
        status, info = login_service.edit_password(id, password, username)
        if status:
            # return Response(status, "密码修改成功")
            return JsonResponse.success(info.msg)
        else:
            return JsonResponse.error(info.msg)

    else:
        # return Response(200, "GET打开成功")
        return JsonResponse.success("GET请求成功")


@bp.route('/get_config', methods=["GET", "POST"])
def get_config():
    id = request.json.get('id')
    print(id)
    status, info = mail_service.get_config(id)
    if status:
        return JsonResponse.success("查询成功", info.data)


@bp.route('/update_config', methods=["POST"])
def update_config():
    # 获取前端传来的账号密码
    data = request.json
    id = data.get('id')
    type = data.get('type')
    value = data.get('value')
    print(id, type, value)

    if type == 'configEmail':
        email = value
        secretkey = None
    elif type == 'configSecretKey':
        email = None
        secretkey = value
    else:
        return JsonResponse.error("无效的类型")

    status, info = mail_service.update_config(id, email, secretkey)
    if status:
        return JsonResponse.success(info.msg, info.data)


@bp.route('/conn_email', methods=["GET", "POST"])
def conn_email():
    status, info = mail_service.test_conn_email()
    if status:
        return JsonResponse.success(info.msg)
    else:
        return JsonResponse.error(info.msg)
